<template>
  <div class="main-box">
    <div class="table-box">
      <div class="card mb10 pt0 pb0">
        <SelectFilter :data="selectFilterData" :default-values="selectFilterValues" @change="changeSelectFilter" />
      </div>
      <ProTable
        ref="proTable"
        title="出库单列表"
        highlight-current-row
        :columns="columns"
        :request-api="getEntryOutList"
        :init-param="Object.assign(initParam, selectFilterValues)"
      >
        <!-- 表格 header 按钮 -->
        <template #tableHeader>
          <el-button type="primary" :icon="Download" plain @click="downloadFile">导出出库单数据</el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation="scope">
          <el-button type="primary" link :icon="View" @click="outWareDetail(scope.row)">查看</el-button>
          <el-button type="primary" link :icon="Van" @click="doChangeCirculationStatus(scope.row)">出库</el-button>
        </template>
      </ProTable>
      <StatusDialog ref="statusRef" />
      <DetailDrawer ref="detailRef" />
      <ImportExcel ref="dialogRef" />
    </div>
  </div>
</template>
<script setup lang="tsx" name="useSelectFilter">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import ImportExcel from "@/components/ImportExcel/index.vue";
import SelectFilter from "@/components/SelectFilter/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { Van, Download, View } from "@element-plus/icons-vue";
import { getEntryOutList, outbound, exportEntryOutInfo } from "@/api/modules/warehouse";
import { Substation, Warehouse } from "@/api/interface";
import { circulationStatus, circulationDetail, circulationType } from "@/utils/serviceDict";
import { useUserStore } from "@/stores/modules/user";
import { useHandleData } from "@/hooks/useHandleData";
import { useDownload } from "@/hooks/useDownload";
import DetailDrawer from "@/views/warehouse/outWare/components/DetailDrawer.vue";

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// 表格配置项
const columns: ColumnProps[] = [
  { prop: "circulationId", label: "单号", width: 90, sortable: true, search: { el: "input" } },
  {
    prop: "goodsItems",
    label: "商品名称",
    render: scope => {
      return (
        <div>
          <text>{scope.row!.goodsItems?.[0].goodsName}</text>
        </div>
      );
    },
    width: 150
  },
  {
    prop: "goodsItems",
    label: "数量",
    render: scope => {
      return (
        <div>
          <text>{scope.row!.goodsItems?.[0].count}</text>
        </div>
      );
    },
    width: 100
  },
  {
    prop: "goodsItems",
    label: "单位",
    render: scope => {
      return (
        <div>
          <text>{scope.row!.goodsItems?.[0].measureUnit}</text>
        </div>
      );
    },
    width: 120
  },
  { prop: "circulationType", label: "流动类型", width: 100, tag: true, enum: circulationType },
  { prop: "circulationDetail", label: "流动细节", width: 120, tag: true, enum: circulationDetail },
  { prop: "circulationStatus", label: "状态", width: 100, tag: true, enum: circulationStatus },
  { prop: "createTime", label: "创建时间", sortable: true, width: 200 },
  { prop: "operation", label: "操作", fixed: "right", width: 278 }
];

// selectFilter 数据
const selectFilterData = reactive([
  {
    title: "流动细节(单)",
    key: "circulationDetail",
    options: [
      {
        label: "全部",
        value: -1
      },
      {
        label: "调拨出库",
        value: 3,
        icon: "Van"
      },
      {
        label: "退货出库",
        value: 4,
        icon: "Box"
      },
      {
        label: "取货出库",
        value: 5,
        icon: "Goods"
      }
    ]
  },
  {
    title: "状态(多)",
    key: "circulationStatus",
    multiple: true,
    options: [
      {
        label: "全部",
        value: -1
      },
      {
        label: "进行中",
        value: 0,
        icon: "Loading"
      },
      {
        label: "已完成",
        value: 1,
        icon: "CircleCheck"
      },
      {
        label: "已取消",
        value: 2,
        icon: "CircleClose"
      }
    ]
  }
]);

// 初始化方法，挂载之后执行的东西填到里面
onMounted(() => {});

// 库房id做为初值
const userStore = useUserStore();
const initParam = reactive({ warehouseId: userStore.userInfo.departmentId, circulationType: 1 });

// 默认 selectFilter 参数
const selectFilterValues = ref({ circulationDetail: -1, circulationStatus: [0] });
const changeSelectFilter = (value: typeof selectFilterValues.value) => {
  ElMessage.success("筛选成功 🤔");
  proTable.value!.pageable.current = 1;
  selectFilterValues.value = value;
};

// 导出出入库单列表
const downloadFile = async () => {
  ElMessageBox.confirm("确认导出数据?", "温馨提示", { type: "warning" }).then(() =>
    useDownload(exportEntryOutInfo, "出库单列表", proTable.value?.searchParam)
  );
};

const outWareDetail = (row: Warehouse.ResEntryOutList) => {
  openDetailDrawer(row);
};

// 更改状态判断
const doChangeCirculationStatus = async (row: Warehouse.ResEntryOutList) => {
  if (row.circulationStatus == 1) {
    ElMessage.error("货物已出库");
  } else if (row.circulationStatus == 2) {
    ElMessage.error("已取消出库");
  } else {
    await useHandleData(outbound, { circulationId: row.circulationId, warehouseId: row.warehouseId }, `出库`);
    proTable.value?.getTableList();
  }
};

const detailRef = ref<InstanceType<typeof DetailDrawer> | null>(null);
const openDetailDrawer = (row: Partial<Substation.ResTaskOrderList> = {}) => {
  const params = {
    title: "出库单详情",
    isView: true,
    row: { ...row },
    api: getEntryOutList,
    getTableList: proTable.value?.getTableList
  };
  detailRef.value?.acceptParams(params);
};
</script>
